const { sequelize } = require('../utils/sequelize');

class NoticeService {
    constructor() {
    }

    async findAll(pageSize, pageNum){
        let data = null;
        let offset = pageSize * (pageNum - 1);
        data = await sequelize.models.Notice.findAndCountAll({
            offset,
            limit: pageSize * 1,
        });
        return data;
    }

    async findById(id){
        let data = null;
        data = await sequelize.models.Notice.findOne({
            where: {id: id}});
        
        return data;
    }

    async saveNotice(obj){
        let data = null;
        data = await sequelize.models.Notice.create(obj)
        return data;
    }

    async deleteNotice(id){
        let data = null;
        data = await sequelize.models.Notice.destroy({
            where: {id: id}
        });
        return data;
    }

    async updateNotice(obj){
        let data = null;
        let noticeObj = {
            title: obj.title,
            content: obj.content,
            img: obj.img,
        }
        data = await sequelize.models.Notice.update({...noticeObj}, {
            where: {id: obj.id}
        })
        return data;
    }
}

module.exports = new NoticeService();